//
//  BToastView.m
//  WBPalm
//
//  Created by 邹 达 on 11-12-7.
//  Copyright 2011 zywx. All rights reserved.
//

#import "BToastView.h"
#import <QuartzCore/CALayer.h>


@implementation BToastView

@synthesize mIndicatorView;
@synthesize mTextView;
@synthesize mPos;

+ (CGRect)viewRectWithPos:(int)inPos wndWidth:(float)inWndWidth wndHeight:(float)inWndHeight {
	CGRect toastViewRect;
	int width = 0;
	int height = 0;
 
		width = F_TOAST_VIEW_WIDTH_PHONE;
		height = F_TOAST_VIEW_HEIGHT_PHONE;
	switch (inPos) {
		case 1:
			toastViewRect = CGRectMake(0, 0, width, height);
			break;
		case 2:
			toastViewRect = CGRectMake((inWndWidth-width)/2, 0, width, height);
			break;
		case 3:
			toastViewRect = CGRectMake(inWndWidth-width, 0, width, height);
			break;
		case 4:
			toastViewRect = CGRectMake(0, (inWndHeight-height)/2, width, height);
			break;
		case 5:
			toastViewRect = CGRectMake((inWndWidth-width)/2, (inWndHeight-height)/2, width, height);
			break;
		case 6:
			toastViewRect = CGRectMake(inWndWidth-width, (inWndHeight-height)/2, width, height);
			break;
		case 7:
			toastViewRect = CGRectMake(0, inWndHeight-height, width, height);
			break;
		case 8:
			toastViewRect = CGRectMake((inWndWidth-width)/2, inWndHeight-height, width, height);
			break;
		case 9:
			toastViewRect = CGRectMake(inWndWidth-width, inWndHeight-height, width, height);
			break;
		default:
			break;
	}
	return toastViewRect;
}

- (void)setSubviewsFrame:(CGRect)inRect {
	float textViewHeight = F_TEXTVIEW_POX_HEIGHT_PHONE;
	if (0) {
		textViewHeight = F_TEXTVIEW_POX_HEIGHT_PAD;
	}
	if (mIndicatorView) {
		[mIndicatorView setFrame:CGRectMake((self.bounds.size.width-mIndicatorView.bounds.size.width)/2, (self.bounds.size.height-textViewHeight-mIndicatorView.bounds.size.height)/2+5, mIndicatorView.bounds.size.width, mIndicatorView.bounds.size.height)];
		[mTextView setFrame:CGRectMake(0, self.bounds.size.height-textViewHeight, self.bounds.size.width, textViewHeight)];
	} else {
		[mTextView setFrame:CGRectMake(0, textViewHeight/2, self.bounds.size.width, textViewHeight)];
	}

}

- (id)initWithFrame:(CGRect)frame Type:(int)inType Pos:(int)inPos{
    
    self = [super initWithFrame:frame];
    if (self) {
		mPos = inPos;
		float textViewHeight = F_TEXTVIEW_POX_HEIGHT_PHONE;
		if (0) {
			textViewHeight = F_TEXTVIEW_POX_HEIGHT_PAD;
		}
		switch (inType) {
			case ToastTypeLoading:
				self.backgroundColor = [UIColor blackColor];
				self.alpha = 0.6;
				[self.layer setMasksToBounds:YES];	
				[self.layer setCornerRadius:6.0]; 
				mIndicatorView =  [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
				[mIndicatorView setFrame:CGRectMake((frame.size.width-mIndicatorView.bounds.size.width)/2, (frame.size.height-textViewHeight-mIndicatorView.bounds.size.height)/2+5, mIndicatorView.bounds.size.width, mIndicatorView.bounds.size.height)];
				[mIndicatorView startAnimating];
				[self addSubview:mIndicatorView];
				mTextView = [[UILabel alloc]initWithFrame:CGRectMake(0, frame.size.height-textViewHeight, frame.size.width, textViewHeight)];
				//mTextView.editable = NO;
				mTextView.backgroundColor = [UIColor clearColor];
				mTextView.textAlignment = UITextAlignmentCenter;
				mTextView.textColor = [UIColor whiteColor];
				mTextView.numberOfLines = 2;
				if (0) {
					mTextView.font = [UIFont systemFontOfSize:F_TEXTVIEW_FONT_SIZE_PAD];
				} else {
					mTextView.font = [UIFont systemFontOfSize:F_TEXTVIEW_FONT_SIZE_PHONE];
				}
				[self addSubview:mTextView];
				break;
			case ToastTypePlainText:
				self.backgroundColor = [UIColor colorWithRed:49/255.0 green:49/255.0 blue:49/255.0 alpha:1.0];
				[self.layer setMasksToBounds:YES];	
				[self.layer setCornerRadius:6.0]; 
				mTextView = [[UILabel alloc]initWithFrame:CGRectMake(10, textViewHeight/2, frame.size.width-20, textViewHeight)];
				//mTextView.editable = NO;
				mTextView.backgroundColor = [UIColor clearColor];
				mTextView.textAlignment = NSLineBreakByWordWrapping;
				mTextView.textColor = [UIColor whiteColor];
                mTextView.lineBreakMode = NSLineBreakByWordWrapping;
				mTextView.numberOfLines = 3;
				if (0) {
					mTextView.font = [UIFont systemFontOfSize:F_TEXTVIEW_FONT_SIZE_PAD];
				} else {
					mTextView.font = [UIFont systemFontOfSize:F_TEXTVIEW_FONT_SIZE_PHONE];
				}
				[self addSubview:mTextView];
				break;
			default:
				break;
		}

    }
    return self;
}
//******2013.1.21--绑定手机号提示语比较长时用到******//
- (id)initWithFrame2:(CGRect)frame Type:(int)inType Pos:(int)inPos{
    
    self = [super initWithFrame:frame];
    if (self) {
		mPos = inPos;
		float textViewHeight = F_TEXTVIEW_POX_HEIGHT_PHONE2;
		if (0) {
			textViewHeight = F_TEXTVIEW_POX_HEIGHT_PAD;
		}
		switch (inType) {
			case ToastTypeLoading:
				self.backgroundColor = [UIColor blackColor];
				self.alpha = 0.6;
				[self.layer setMasksToBounds:YES];
				[self.layer setCornerRadius:6.0];
				mIndicatorView =  [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
				[mIndicatorView setFrame:CGRectMake((frame.size.width-mIndicatorView.bounds.size.width)/2, (frame.size.height-textViewHeight-mIndicatorView.bounds.size.height)/2+5, mIndicatorView.bounds.size.width, mIndicatorView.bounds.size.height)];
				[mIndicatorView startAnimating];
				[self addSubview:mIndicatorView];
				mTextView = [[UILabel alloc]initWithFrame:CGRectMake(0, frame.size.height-textViewHeight, frame.size.width, textViewHeight)];
				//mTextView.editable = NO;
				mTextView.backgroundColor = [UIColor clearColor];
				mTextView.textAlignment = UITextAlignmentCenter;
				mTextView.textColor = [UIColor whiteColor];
				mTextView.numberOfLines = 2;
				if (0) {
					mTextView.font = [UIFont systemFontOfSize:F_TEXTVIEW_FONT_SIZE_PAD];
				} else {
					mTextView.font = [UIFont systemFontOfSize:F_TEXTVIEW_FONT_SIZE_PHONE];
				}
				[self addSubview:mTextView];
				break;
			case ToastTypePlainText:
				self.backgroundColor = [UIColor colorWithRed:49/255.0 green:49/255.0 blue:49/255.0 alpha:1.0];
				[self.layer setMasksToBounds:YES];
				[self.layer setCornerRadius:6.0];
				mTextView = [[UILabel alloc]initWithFrame:CGRectMake(10, textViewHeight/2 - 40, frame.size.width-20, textViewHeight)];
				//mTextView.editable = NO;
				mTextView.backgroundColor = [UIColor clearColor];
				mTextView.textAlignment = UITextAlignmentCenter;
				mTextView.textColor = [UIColor whiteColor];
                mTextView.lineBreakMode = NSLineBreakByWordWrapping;
				mTextView.numberOfLines = 4;
				if (0) {
					mTextView.font = [UIFont systemFontOfSize:F_TEXTVIEW_FONT_SIZE_PAD];
				} else {
					mTextView.font = [UIFont systemFontOfSize:F_TEXTVIEW_FONT_SIZE_PHONE];
				}
				[self addSubview:mTextView];
				break;
			default:
				break;
		}
        
    }
    return self;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
 	mIndicatorView = nil;
 	mTextView = nil;
}


@end
